import React from 'react';
import { Button,  Input} from 'antd';
import Bread from '../../component/TableContent/VipBread';
import axios from '../../utils/http'
import './LookAccount.css'
//会员等级
class VipLevel extends React.Component {
  constructor() {
    super()
    this.state= {
      inputval: '',
      tableData:[]
    }
  }
  gettabledata =  () => {
    console.log('刷新')
    axios({
      url: '/member/queryMemLevel',
      method:'get'
    }).then(({ data }) => {
      console.log(data.data)
      for (let i = 0; i < data.data.length; i++){
        data.data[i].key=i
      }
      this.setState({
        tableData:data.data
      })
    })
  }
  
  getvalu = e => {
    this.setState({
      inputval: e.target.value,
    },
    )
    axios({
      url: '/member/updateMemLevel',
      method: 'get',
      params: {
        memLevelRule:e.target.value
      }
    }).then(data => {
      if(data.data.code===200)
      window.location.reload([true])
    })
  }
  // foraddLevel = (num) => {
  //   for (let i = 0; i < num; i++){
  //     console.log(i)
  //     this.addLevel1(i)
  //   }
  // }
  /* addLevel1 = (i) => {
    console.log(i)
    let levelname = 'v' + parseInt( i+ 1 )
    let xia, shang
    if (i<=0) {
      xia = 0
      shang=parseInt(this.state.inputval)
    } else {
      xia=parseInt(i) * parseInt(this.state.inputval)+1
      shang = parseInt(this.state.inputval) + parseInt(i) * parseInt(this.state.inputval)
    }
    // console.log(levelname)
    // console.log(xia)
    // console.log(shang)
    axios({
      url: '/member/addMemLevel',
      method: 'post',
      data: {
        memLevelif: xia,//下限
        memLeveluf: shang,//上限
        memLevelname: levelname
      }
    }).then(data => {
      console.log(data) 
      
    })
    this.gettabledata()
  } */
  addLevel = () => {
    console.log(parseInt(this.state.tableData.length + 1))
    let levelname = 'v' + parseInt(this.state.tableData.length + 1) 
    let xia, shang
    if (this.state.tableData.length <=0) {
      xia = 0
      shang=parseInt(this.state.inputval)
    } else {
      xia=parseInt(this.state.tableData.length) * parseInt(this.state.inputval)+1
      shang = parseInt(this.state.inputval) + parseInt(this.state.tableData.length) * parseInt(this.state.inputval)
    }
    // console.log(levelname)
    // console.log(xia)
    // console.log(shang)
    axios({
      url: '/member/addMemLevel',
      method: 'post',
      data: {
        memLevelif: xia,//下限
        memLeveluf: shang,//上限
        memLevelname: levelname
      }
    }).then(data => {
      // console.log(data)
      this.gettabledata()
    })
    console.log('增加结束')
  }
  delalldata = () => {
      axios({
      url: '/member/deleteMemLevel',
      method:'get'
    }).then(({data}) => {
      this.gettabledata()
      return data.code
    })
  }
  delonedata = () => {
    axios({
      url: '/member/deleteMemLevel',
      method: 'get',
      params: {
        mem_levelname:'v'+this.state.tableData.length
      }
    }).then(data => {
      console.log(data)
      this.gettabledata()
    })
  }
  renderData = () => {
    return this.state.tableData.map((item, i) => {
      return <tr key={ item.key}>
        <td>{item.memLevelname}</td>
        <td><Input disabled style={{ width: '8rem', textAlign: 'center' }} defaultValue={item.memLevelif }></Input></td>
        <td><Input disabled style={{ width: '8rem',textAlign:'center' }} defaultValue={item.memLeveluf }></Input></td>
      </tr>
    })
  }
  render() {
  return (
    <div>
      <Bread mianbao='会员等级'></Bread>
    <div className='wrapper'>
        请设置等级积分跨度：<Input style={{ width: '10rem' }} defaultValue={this.state.inputval} onPressEnter={this.getvalu}></Input>
      <table style={{width:'100%',color:'#e9e9eb',textAlign:'center',marginTop:'1rem'}} className='mytable'>
        <thead >
          <tr>
            <td>等级名称</td>
            <td>积分下限</td>
            <td>积分上限</td>
          </tr>
        </thead>
          <tbody>
            { this.renderData()}
          
        </tbody>
      </table>
    </div>
    <Button className='surename' onClick={this.addLevel}>添加等级</Button>
    <Button className='surename' onClick={this.delalldata}>删除全部等级</Button>
    <Button className='surename' onClick={this.delonedata}>删除一个等级</Button>
    </div>
  );
  }
  componentDidMount() {
    this.setState({
      inputval:this.state.inputval
    },()=>this.gettabledata()
    )
    
  }
}
export default VipLevel;